using UnityEngine;
using FairyGUI;
using System.Collections.Generic;

namespace ABA
{
    public class SysTips : Singleton<SysTips>
    {
        private class SysTipsWin : FGUIWindow
        {

            public SysTipsWin(FGUIWinInfo type) : base(type)
            {
            }

            protected override void OnInit()
            {
                base.OnInit();
                Center();
                //不能阻挡点击事件
                SetBlockEvent(false, null);
            }

            private void _SetFontForTF(GTextField tf, string name){
                var tft = tf.textFormat;
                tft.font = name;
                tf.textFormat = tft; 
            }   

            public void SetFont(string name)
            {
                bool bShow = false;
                if (contentPane == null) bShow = true;

                if (bShow) Show();
                
                // _SetFontForTF(contentPane.GetChild("txt").asTextField, name);

                var childs = contentPane.GetChildren();
                for (int i = 0; i < childs.Length; i++)
                {
                    GComponent child = childs[i] as GComponent;
                    _SetFontForTF(child.GetChild("txt").asTextField, name);
                }

                if (bShow) HideImmediately();
            }

            GComponent GetTipFromPool()
            {
                GComponent ret = null;

                var childs = contentPane.GetChildren();
                for (int i = 0; i < childs.Length; i++)
                {
                    var child = childs[i];
                    if(child.visible == false){
                        ret = child as GComponent;
                        break;
                    }
                }

                return ret;
            }

            public void SetContent(string strText,Color color)
            {
                if(contentPane == null)
                {
                    Show();
                }
                
                var tip = GetTipFromPool();
                if(tip == null) return;

                if(!isShowing) Show();
                tip.visible = true;

                GTextField xx = tip.GetChild("txt").asTextField;
                xx.text = strText;
                xx.color = color;

                Transition t = tip.GetTransition("t0");
                t.Stop();
                t.Play(() =>
                {
                    tip.visible = false;
                    bool isAllHide = true;
                    var childs = contentPane.GetChildren();
                    for (int i = 0; i < childs.Length; i++)
                    {
                        var child = childs[i];
                        if(child.visible == true){
                            isAllHide = false;
                            break;
                        }
                    }      

                    if(isAllHide == true){
                        Hide();
                    }              
                });

                var childs = contentPane.GetChildren();
                for (int i = 0; i < childs.Length; i++)
                {
                    var child = childs[i];
                    if(child.visible == true)
                        child.y -= 85;
                }

                tip.y = -100;
                tip.x = 125;
                contentPane.AddChild(tip);
            }

            // protected override void DoHideAnimation()
            // {
            //     HideImmediately();
            // }

        }

        SysTipsWin wind;
        protected override void Init()
        {
            var SYS_TIPS = new FGUIWinInfo
            {
                cls_name = "TipsWin",
                pkg_name = "sys",
                comp_name = "systips",
                zindex = E_FGUIWIN_ZINDEX.TIPS,
                ani = false
            };

            // wind = new TipsWin(WinConst.SYS_TIPS);
            wind = new SysTipsWin(SYS_TIPS);
        }

        public void Show(string strText,float r =1f, float g=1f, float b=1f, float a=1f)
        {
            wind.SetContent(strText,new Color(r,g,b,a));
        }

        public void Hide()
        {
            wind.Hide();
        }
        public void SetFont(string name)
        {
            wind.SetFont(name);
        }

    }
}
